package com.ss.android.article.base.feature.app.browser;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebView;

import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.Logger;
import com.bytedance.common.utility.UIUtils;
import com.ss.android.account.Spipe;
import com.ss.android.article.base.app.AppData;
import com.ss.android.article.base.feature.app.e.TTAndroidObject;
import com.ss.android.article.base.feature.app.e.TTJsInterface;
import com.ss.android.article.base.feature.e.m;
import com.ss.android.article.base.feature.model.Article;
import com.ss.android.article.base.feature.pgc.PgcActivity;
import com.ss.android.article.base.feature.search.r;
import com.ss.android.article.base.feature.subscribe.model.EntryItem;
import com.ss.android.article.base.feature.video.MediaHelper;
import com.ss.android.article.base.ui.BaseActionDialog;
import com.ss.android.article.common.BaseBrowserFragment;
import com.ss.android.article.common.share.factory.ShareHelperFactory;
import com.ss.android.article.common.share.helper.CommonWXShareHelper;
import com.ss.android.article.share.e.b;
import com.ss.android.article.share.entity.BaseShareContent;
import com.ss.android.article.share.entity.ShareAction;
import com.ss.android.common.lib.MobClickCombiner;
import com.ss.android.common.callback.SSCallback;
import com.ss.android.image.FrescoUtils;
import com.ss.android.image.loader.LargeImageCtxRef;
import com.ss.android.newmedia.a.BrowserFragment;
import com.ss.android.newmedia.activity.browser.BrowserActivity;
import com.ss.android.newmedia.e.JsObject;
import com.ss.android.newmedia.util.AppUtil;

import java.lang.ref.WeakReference;
import java.util.EnumSet;

import org.json.JSONObject;

public class GetEntryItemHelper extends BaseBrowserFragment implements
        WeakHandler.Callback,
        com.ss.android.newmedia.activity.browser.BrowserActivity.a {

    @Override
    public void f(@NonNull Object o) {

    }

    final class a$a implements b {
        private final GetEntryItemHelper b;
        private Article a;

        public a$a(GetEntryItemHelper arg1, Article arg2) {
            this.b = arg1;

            this.a = arg2;
        }

        private void a(int arg4) {
            CommonWXShareHelper v0 = CommonWXShareHelper.getInstance(this.b.getContext(), AppData.getInstance(), arg4);
            v0.b();
            v0.a(this.a, new Object[0]);
            this.a(CommonWXShareHelper.b(arg4));
        }

        private void a(String arg9) {
            if (!android.support.design.a.isTextEmpty(arg9)) {
                MobClickCombiner.onEvent(this.b.getActivity(), this.b.c, arg9, this.b.b, 0);
            }
        }

        private void a(String arg4, ShareAction arg5) {
            if (this.a != null) {
                ShareHelperFactory.getInstance();
                if (ShareHelperFactory.getActionHelper(this.b.getContext(), arg5, AppData.getInstance()).doAction(this.a, new Object[0])) {
                    this.a(arg4);
                }
            }
        }

        public final boolean a(com.ss.android.article.share.entity.a arg4, View arg5, com.ss.android.article.share.c.a arg6) {
            boolean v0 = true;
            boolean v1 = false;
            if (arg4 != null) {
                switch (arg4.e) {
                    case 1: {
                        this.a(1);
                        break;
                    }
                    case 2: {
                        this.a(0);
                        break;
                    }
                    case 3: {
                        this.a("share_qq", ShareAction.qq);
                        break;
                    }
                    case 10: {
                        this.a("share_system", ShareAction.link);
                        break;
                    }
                    default: {
                        v0 = false;
                        break;
                    }
                }

                v1 = v0;
            }

            return v1;
        }
    }

    public final class a$b extends TTJsInterface {
        private final GetEntryItemHelper a;

        protected a$b(GetEntryItemHelper arg1) {
            this.a = arg1;

        }

        public final void a(long arg8) {
            Object v0;
            long v4 = 0;
            Object v1 = null;
            if ((this.a.isActive()) && arg8 > v4) {
                com.ss.android.article.base.feature.pgc.GetEntryItemHelper v2 = com.ss.android.article.base.feature.pgc.GetEntryItemHelper.a();
                UIUtils.a();
                if (arg8 <= v4) {
                    Logger.w("GetEntryItemHelper", "getEntryItem -  mMediaId <= 0");
                    v0 = v1;
                } else {
                    v0 = v2.a.get(Long.valueOf(arg8));
                    if (v0 != null && ((EntryItem) v0).mId == arg8 && !android.support.design.a.isTextEmpty(((EntryItem) v0).mShareUrl)) {
                        Logger.d("GetEntryItemHelper", "getEntryItem -  have get data from cache");
//                        goto label_14;
                    } else {
                        EntryItem v0_1 = EntryItem.optObtain(arg8);
                        if (v0_1 != null && v0_1.mId == arg8 && !android.support.design.a.isTextEmpty(v0_1.mShareUrl)) {
                            Logger.d("GetEntryItemHelper", "getEntryItem -  have get data from EntryItem cache ");
                            v2.a.put(Long.valueOf(arg8), v0_1);
//                        goto label_14;
                        } else {
                            Logger.d("GetEntryItemHelper", "getEntryItem -  not get data");
                            v0 = v1;
                        }
                    }
                }

                label_14:
                if (v0 != null) {
                    this.a.a(((EntryItem) v0));
                    return;
                }

                com.ss.android.article.base.feature.pgc.GetEntryItemHelper.a().a(arg8, new c(arg8, this.a));
            }
        }

        public final void a(long arg8, String arg10) {
            long v4 = 0;
            if ((this.a.isActive()) && arg8 > v4) {
                this.a.b = arg8;
                this.a.c = arg10;
                new com.ss.android.article.base.feature.app.browser.g(this.a.a, new Article(arg8, v4, 0)).start();
            }
        }

        public final void a(BaseShareContent arg3) {
            this.a.d = arg3;
            if ((TextUtils.isEmpty(this.a.d.getTargetUrl())) || (this.a.d.getTargetUrl().equals("about:blank"))) {
                this.a.d.setTargetUrl(GetEntryItemHelper.a(this.a));
            }

            this.a.d.setTargetUrl(a.b(this.a.d.getTargetUrl()));
            if ((TextUtils.isEmpty(this.a.d.getText())) || (this.a.d.getText().equals("about:blank"))) {
                this.a.d.setText(this.a.d.getTargetUrl());
            }

            FrescoUtils.preload(Uri.parse(arg3.getMedia().getUrl()), this.a.getContext());
        }

        public final void a(String arg2) {
            if ((this.a.isActive()) && !TextUtils.isEmpty(((CharSequence) arg2))) {
                GetEntryItemHelper.a(this.a, arg2);
            }
        }
    }

    final class c implements SSCallback {
        private long a;
        private WeakReference b;

        public c(long arg2, GetEntryItemHelper arg4) {
            this.a = arg2;
            this.b = new WeakReference(arg4);
        }

        public final Object onCallback(Object[] args) {
            int v7 = 2;
            Object v6 = null;
            if (this.b != null && this.b.get() != null) {
                Object v0 = this.b.get();
                if ((((GetEntryItemHelper) v0).isActive()) && ((Integer) args[0]).intValue() != v7 && ((Long) args[1]).longValue() == this.a) {
                    ((GetEntryItemHelper) v0).a(((EntryItem) args[v7]));
                }
            }

            return v6;
        }
    }

    final class a$d implements b {
        private final GetEntryItemHelper c;
        private EntryItem a;
        private boolean b;

        public a$d(GetEntryItemHelper arg5, EntryItem arg6) {
            this.c = arg5;
            this.a = arg6;
            boolean v0 = Spipe.inst().pgcMediaid == this.a.mId ? true : false;
            this.b = v0;
        }

        private void a(int arg4) {
            CommonWXShareHelper v0 = CommonWXShareHelper.getInstance(this.c.getContext(), AppData.getInstance(), arg4);
            v0.b();
            v0.a(this.a, new Object[0]);
            this.a(CommonWXShareHelper.b(arg4));
        }

        private void a(String arg9) {
            long v6 = 0;
            if (!android.support.design.a.isTextEmpty(arg9)) {
                long v4 = this.a != null ? this.a.mId : v6;
                MobClickCombiner.onEvent(this.c.getActivity(), "pgc_profile", arg9, v4, v6);
            }
        }

        private void a(String arg4, ShareAction arg5) {
            if (this.a != null) {
                ShareHelperFactory.getInstance();
                if (ShareHelperFactory.getActionHelper(this.c.getContext(), arg5, AppData.getInstance()).doAction(this.a, new Object[0])) {
                    this.a(arg4);
                }
            }
        }

        public final boolean a(com.ss.android.article.share.entity.a arg13, View arg14, com.ss.android.article.share.c.a arg15) {
            boolean v0_1;
            int v11 = 140;
            int v10 = 3;
            int v9 = 2;
            boolean v5 = false;
            if (arg13 != null) {
                int v6 = arg13.e;
                if (this.a != null) {
                    String v0 = this.a.mName != null ? this.a.mName : "";
                    String v1 = this.a.mDescription != null ? this.a.mDescription : "";
                    String v2 = this.a.mShareUrl != null ? this.a.mShareUrl : "";
                    String v3 = this.b ? this.c.getString(com.ss.android.article.browser.R.string.s5) : this.c.getString(com.ss.android.article.browser.R.string.s6);
                    Object[] v7 = new Object[v10];
                    v7[0] = v0;
                    v7[1] = v1;
                    v7[v9] = v2;
                    String v7_1 = String.format(v3, v7);
                    switch (v6) {
                        case 5:
                        case 6: {
                            if (v7_1.length() <= v11) {
//                        goto label_36;
                            } else {
                                v7 = new Object[v10];
                                v7[0] = v0;
                                v7[1] = "";
                                v7[v9] = v2;
                                v7_1 = String.format(v3, v7);
                                int v8 = 140 - v7_1.length();
                                if (v8 > 0) {
                                    v7 = new Object[v10];
                                    v7[0] = v0;
                                    v7[1] = v1.substring(0, Math.min(v8 - 1, v1.length()));
                                    v7[v9] = v2;
                                    String.format(v3, v7);
//                        goto label_36;
                                } else {
                                    v7_1.substring(0, v11);
                                }
                            }
                            break;
                        }
                    }
                }

                label_36:
                switch (v6) {
                    case 1: {
                        this.a(1);
                        v0_1 = true;
                        break;
                    }
                    case 2: {
                        this.a(0);
                        v0_1 = true;
                        break;
                    }
                    case 3: {
                        this.a("share_qq", ShareAction.qq);
                        v0_1 = true;
                        break;
                    }
                    case 4: {
                        this.a("share_qzone", ShareAction.qzone);
                        v0_1 = true;
                        break;
                    }
                    case 10: {
                        this.a("share_system", ShareAction.link);
                        v0_1 = true;
                        break;
                    }
                    default: {
                        v0_1 = false;
                        break;
                    }
                }

                v5 = v0_1;
            }

            return v5;
        }
    }

    public static final class a$e {
        private final r a;

        public a$e(r arg1) {
            this.a = arg1;

        }
    }

    private boolean R;
    private TTJsInterface S;
    private m T;
    private Article U;
    private static String[] V;
    WeakHandler a;
    long b;
    String c;
    BaseShareContent d;
    public a$e e;

    static {
        GetEntryItemHelper.V = new String[]{"iid", "device_id", "ac", "channel", "aid", "app_name", "version_code", "version_name", "device_platform", "ab_version", "ab_client", "ab_group", "ab_feature", "abflag", "ssmix", "device_type", "device_brand", "language", "os_api", "os_version", "uuid", "openudid", "aliyun_uuid", "manifest_version_code", "resolution", "dpi", "update_version_code", "_rticket"};
    }

    public GetEntryItemHelper() {
        this.R = false;
        this.a = new WeakHandler(((WeakHandler.Callback) this));
    }

    static String a(GetEntryItemHelper arg1) {
        return arg1.y;
    }

    static void a(GetEntryItemHelper arg3, String arg4) {
        if (arg3.e != null) {
            a$e v1 = arg3.e;
            v1.a.a(arg4, true);
            v1.a.c.setText(((CharSequence) arg4));
            int v0 = 0;
            if (v1.a.c.getText() != null && !TextUtils.isEmpty(v1.a.c.getText().toString())) {
                v0 = v1.a.c.getText().toString().length();
            }

            v1.a.c.setSelection(v0);
            v1.a.c.dismissDropDown();
        }
    }

    private void a(boolean arg4) {
        if (this.mJsObject != null) {
            try {
                JSONObject v1 = new JSONObject();
                v1.put("code", "1");
                String v0_1 = arg4 ? "visible" : "invisible";
                this.mJsObject.b(v0_1, v1);
            } catch (Exception v0) {
            }
        }
    }

    public final void initTTAndroidObject() {
        if (this.mJsObject == null) {
            com.ss.android.article.base.feature.app.browser.a.a v0 = new com.ss.android.article.base.feature.app.browser.a.a(AppData.getInstance(), this.getContext());
            v0.setLargeImageContext(((LargeImageCtxRef) this));
            v0.setWebView(this.mWebview);
            v0.setFragment(((Fragment) this));
            this.mJsObject = ((JsObject) v0);
        }
    }

    public final void a(Uri arg7, WebView arg8) {
        super.a(arg7, arg8);
        if ("video".equals(arg7.getHost())) {
            MobClickCombiner.onEvent(this.getActivity(), "video", "play");
            MediaHelper.startMediaPlayerActivity(this.getActivity(), arg7.getQueryParameter("play_url"), arg7.getQueryParameter("json"), arg8.getUrl(), AppUtil.getUserAgent(this.getActivity(), arg8), null);
        }
    }

    final void a(EntryItem arg11) {
        int v7 = -2;
        if (arg11 != null) {
            String v4 = Spipe.inst().pgcMediaid == arg11.mId ? "my_pgc_profile" : "pgc_profile";
            EnumSet v6 = EnumSet.of(BaseActionDialog.CtrlFlag.disableAlipay);
            if (this.T.a()) {
                v6.add(BaseActionDialog.CtrlFlag.hasWeixin);
            }

            BaseActionDialog v0 = new BaseActionDialog(this.getActivity(), new a$d(this, arg11), 205, v4, BaseActionDialog.DisplayMode.SHARE, v6);
            v0.getWindow().setLayout(v7, v7);
            v0.show();
            v0.e = arg11.mId;
            MobClickCombiner.onEvent(this.getActivity(), v4, "share_button", arg11.mId, 0);
        }
    }

    public final void a(String arg3) {
        if (!android.support.design.a.isTextEmpty(arg3)) {
            try {
                Intent v0_1 = new Intent();
                v0_1.setData(Uri.parse(GetEntryItemHelper.b(arg3)));
                v0_1.setAction("android.intent.action.VIEW");
                this.startActivity(v0_1);
            } catch (Exception v0) {
                v0.printStackTrace();
            }
        }
    }

    public static String b(String arg5) {
        String[] v1 = GetEntryItemHelper.V;
        int v0;
        for (v0 = 0; v0 < 28; ++v0) {
            arg5 = arg5.replaceAll("(?<=[\\?&])" + v1[v0] + "=[^&]*&?", "");
        }

        return arg5.replaceAll("&+$", "");
    }

    public final void onPageFinished() {
        super.onPageFinished();
        if ((this.getActivity() instanceof BrowserActivity)) {
            this.mWebview.loadUrl("javascript:(function(){" +
                    "function loadScript(url,callback){" +
                    "var head=document.head,script;" +
                    "script=document.createElement(\'script\');" +
                    "script.async=false;" +
                    "script.type=\'text/javascript\';" +
                    "script.charset=\'utf-8\';" +
                    "script.src=url;" +
                    "head.insertBefore(script,head.firstChild);" +
                    "if(callback){script.addEventListener(\'load\',callback,false)}}" +
                    "function sendMsg(argument){" +
                    "var min_image_size=100;" +
                    "var title=\'\',desc=\'\',icon=\'\',title_ele=document.querySelector(\'title\'),desc_ele=document.querySelector(\'meta[name=description]\');" +
                    "if(title_ele){title=title_ele.innerText}if(desc_ele){desc=desc_ele.content}" +
                    "var imgs=document.querySelectorAll(\'body img\');" +
                    "for(var i=0;i<imgs.length;i++){" +
                    "var img=imgs[i];" +
                    "if(img.naturalWidth>min_image_size&&img.naturalHeight>min_image_size){" +
                    "icon=img.src;" +
                    "break}}" +
                    "window.ToutiaoJSBridge.call(\'shareInfo\',{\'title\':title,\'desc\':desc,\'image\':icon,\'url\':location.href})}" +
                    "if(!window.ToutiaoJSBridge){var protocol=location.protocol.indexOf(\'https\')>-1?\'https://\':\'http://\';" +
                    "loadScript(protocol+\'s2.pstatp.com/inapp/toutiao.js\',sendMsg)}else{sendMsg()}})();");
        }
    }

    public final void c() {
        MobClickCombiner.onEvent(this.getContext(), "wap_share", "share_button");
        BaseActionDialog baseActionDialog = new BaseActionDialog(
                this.getActivity(),
                new com.ss.android.article.base.feature.app.browser.b(this),
                0,
                null,
                BaseActionDialog.DisplayMode.SHARE,
                null);
        baseActionDialog.getWindow().setLayout(-2, -2);
        baseActionDialog.show();
    }

    public final boolean d() {
        boolean v0 = this.d == null || (this.F) ? false : true;
        return v0;
    }

    public final void onPullToRefresh() {
        if (this.isViewValid()) {
            if (!android.support.design.a.isTextEmpty(this.mBaseUrl)) {
                boolean v0 = this.A ? this.mBaseUrl.contains("tt_daymode=1") : this.mBaseUrl.contains("tt_daymode=0");
                if (!v0) {
//                    goto label_32;
                } else {
                    String v0_1 = this.A ? this.mBaseUrl.replaceAll("tt_daymode=1", "tt_daymode=0") : this.mBaseUrl.replaceAll("tt_daymode=0", "tt_daymode=1");
                    this.mBaseUrl = v0_1;
                    ((BrowserFragment) this).a(this.mBaseUrl, false);
                    return;
                }
            }

            label_32:
            this.m();
        }
    }

    public void handleMsg(Message message) {
        int v8 = -2;
        if (this.isActive()) {
            if (message.what != 10) {
            } else if ((message.obj instanceof Article)) {
                this.U = ((Article) message.obj);
                Article v7 = this.U;
                if (v7 != null) {
                    String v4 = this.c;
                    EnumSet v6 = EnumSet.of(BaseActionDialog.CtrlFlag.disableAlipay);
                    if (this.T.a()) {
                        v6.add(BaseActionDialog.CtrlFlag.hasWeixin);
                    }

                    BaseActionDialog v0 = new BaseActionDialog(this.getActivity(), new a$a(this, v7), 200, v4, BaseActionDialog.DisplayMode.SHARE, v6);
                    v0.getWindow().setLayout(v8, v8);
                    v0.show();
                    MobClickCombiner.onEvent(this.getActivity(), v4, "share_button", v7.groupId, 0);
                }
            }
        }
    }

    public void onActivityCreated(Bundle bundle) {
        Bundle v0 = this.getArguments();
        if (v0 != null) {
            v0.putBoolean("enable_pull_refresh", v0.getBoolean("enable_pull_refresh", false));
        }

        super.onActivityCreated(bundle);
        if ((this.mJsObject instanceof TTAndroidObject)) {
            this.S = new a$b(this);
            if (this.S != null) {
                ((TTAndroidObject) this.mJsObject).setTTJsInterface(this.S);
            }
        }

        if (((this.mJsObject instanceof com.ss.android.article.base.feature.app.browser.a.a)) && ((this.mContext instanceof com.ss.android.newmedia.activity.browser.d))) {
            ((com.ss.android.article.base.feature.app.browser.a.a) this.mJsObject).a = (com.ss.android.newmedia.activity.browser.d) this.mContext;
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        new com.ss.android.article.base.feature.e.i(this.getActivity());
        this.T = m.a(this.getActivity());
    }

    public void onDestroy() {
        TTJsInterface v1 = null;
        super.onDestroy();
        if (((this.mJsObject instanceof TTAndroidObject)) && this.S != null) {
            ((TTAndroidObject) this.mJsObject).setTTJsInterface(v1);
        }

        if ((this.mJsObject instanceof com.ss.android.article.base.feature.app.browser.a.a)) {
            ((com.ss.android.article.base.feature.app.browser.a.a) this.mJsObject).a = ((com.ss.android.newmedia.activity.browser.d) v1);
        }
    }

    public void onPause() {
        super.onPause();
        if (this.R) {
            this.R = false;
            FragmentActivity v0 = this.getActivity();
            if (v0 != null && !((Activity) v0).isFinishing()) {
                this.a(false);
                return;
            }

            if (!(v0 instanceof PgcActivity)) {
                return;
            }

            if (!v0.isFinishing()) {
                return;
            }

            try {
                this.mWebview.clearCache(true);
            } catch (Exception v0_1) {
            }
        }
    }

    public void onResume() {
        super.onResume();
        this.R = true;
        this.a(true);
    }
}

